package com.bitoon.basketdudes.jpa;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;

@Entity
@Table(name = "servicios", schema="catalogo")

@NamedQueries({
    @NamedQuery(name = "Servicio.findAll",            query = "SELECT s FROM Servicio s"),
    @NamedQuery(name = "Servicio.buscarIpPuerto",     query = "SELECT s FROM Servicio s WHERE s.ip = :ip AND s.puerto = :puerto"),
    @NamedQuery(name = "Servicio.buscarIpPuertoTipo", query = "SELECT s FROM Servicio s WHERE s.ip = :ip AND s.puerto = :puerto AND s.tipo = :tipo"),
    // Busca servicios con espacio libre y que estan en estado 0 (Ready)
    @NamedQuery(name = "Servicio.buscarTipo",         query = "SELECT s FROM Servicio s WHERE LOWER(s.tipo) = :tipo and s.libre> 3 and s.status=0 ORDER BY f_ultimo_ping desc,libre desc"),
    /* FUNCIONABA
    @NamedQuery(name = "Servicio.buscarTipoRegion",   query = "SELECT s FROM Servicio s WHERE LOWER(s.tipo) = :tipo and s.libre> 3 and s.status=0 and s.zonaId in (select x.zonaId from RegionZona x where x.regionId=:region_id ) ORDER BY f_ultimo_ping desc,libre desc"),    
    @NamedQuery(name = "Servicio.buscarTipoZona",     query = "SELECT s FROM Servicio s WHERE LOWER(s.tipo) = :tipo and s.libre> 3 and s.status=0 and s.zonaId=:zona ORDER BY f_ultimo_ping desc,libre desc"),
*/
    @NamedQuery(name = "Servicio.buscarTipoRegion",   query = "SELECT s FROM Servicio s WHERE LOWER(s.tipo) = :tipo and s.libre> 3 and s.status=0 and s.zonaId in (select x.zona.id from RegionZona x where x.regionId=:region_id ) ORDER BY f_ultimo_ping desc,libre desc"),    
    @NamedQuery(name = "Servicio.buscarTipoZona",     query = "SELECT s FROM Servicio s WHERE LOWER(s.tipo) = :tipo and s.libre> 3 and s.status=0 and s.zonaId=:zona ORDER BY f_ultimo_ping desc,libre desc"),
    
    @NamedQuery(name = "Servicio.updateId",           query = "UPDATE Servicio s SET s.fechaUltimoPing = current_timestamp() where s.id = :id")
})

public class Servicio implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="servicio_id_generator", sequenceName="catalogo.servicios_id_seq")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="servicio_id_generator")
    @Basic(optional=false)
    private Integer id;

    @Basic(optional = false)
    private String ip;

    @Basic(optional = false)
    private Integer puerto;

    @Basic(optional = false)
    private String tipo;

    @Column(name = "f_alta")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaAlta;

    @Column(name = "f_ultimo_ping")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaUltimoPing;

    private Integer status;

    @Basic(optional = false)
    private Integer total;

    @Basic(optional = false)
    private Integer libre;

    @Basic(optional = false)
    @Column(name = "zona_id")
    private int     zonaId;

    @Basic(optional = false)
    private String nombre;

    public Servicio() {
        this.fechaAlta = new Date();
        this.status = 0;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPuerto() {
        return puerto;
    }

    public void setPuerto(Integer puerto) {
        this.puerto = puerto;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public Date getFechaAlta() {
        return fechaAlta;
    }

    public void setFechaAlta(Date fechaAlta) {
        this.fechaAlta = fechaAlta;
    }

    public Date getFechaUltimoPing() {
        return fechaUltimoPing;
    }

    public void setFechaUltimoPing(Date fechaUltimoPing) {
        this.fechaUltimoPing = fechaUltimoPing;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public int getLibre() {
        return libre;
    }

    public void setLibre(int valor) {
        this.libre = valor;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int valor) {
        this.total = valor;
    }

    public int getZonaId() {
        return zonaId;
    }

    public void setZonaId(int valor) {
        this.zonaId = valor;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String valor) {
        this.nombre = valor;
    }


}

